#!/usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb
import sys
import re

DB_PREFIX = "ru_"
TABLE_PAGE = DB_PREFIX + "page"
TABLE_CATEGORYLINKS = DB_PREFIX + "categorylinks"
TABLE_INFOBOX = DB_PREFIX + "infobox"
TABLE_INFOBOX_CATEGORIES = DB_PREFIX + "infobox_categories"
TABLE_INFOBOX_ATTRIBUTES = DB_PREFIX + "infobox_attrib"
ERROR_DUBLICATE_ENTRY = 1062
#TOP_LEVEL_INFOBOX = (191079, "Википедия:Шаблоны-карточки")

TOP_LEVEL_INFOBOX_CATEGORY = (3108204, "Infobox_templates")
#TOP_LEVEL_INFOBOX_CATEGORY = (191079, "Википедия:Шаблоны-карточки")
#TOP_LEVEL_INFOBOX_CATEGORY = ("2600760", "Шаблоны-карточки:Культура_и_искусство")
NAMESPACE_CATEGORY = "14"
NAMESPACE_TEMPLATE = "10"

def quoted(string):
	return "\"" + string + "\""
#TODO: choose between ' and "


def get_infobox_text(conn1, conn2):

	read_cursor = conn1.cursor()
	write_cursor = conn2.cursor()

	query = "SELECT \
					inf_id, \
					old_text \
				FROM "\
					+ DB_PREFIX + "infobox \
					INNER JOIN " + DB_PREFIX + "page \
						ON inf_id = page_id \
					INNER JOIN " + DB_PREFIX + "text \
						ON old_id = page_latest \
				WHERE \
					page_namespace = " + NAMESPACE_TEMPLATE

	read_cursor.execute(query) 

	#print read_cursor.fetchone()
	
	
	#get everything like {{{name|useless}}}
	regexp = re.compile(u"{{{([-A-Za-zА-Яа-я0-9 _\t]*?)\|.*?}}}")
	columns = ["ia_of", "ia_name"]
	
	while (True):
		row = read_cursor.fetchone()
		
		if row == None:
			break

		text = unicode(row[1], "utf-8")

		attrib_set = set() #for dublicate attributes truncation

		for attrib in regexp.findall(text):
			attrib_set.add(attrib.strip())

		#TODO: delete things like "nocat" here

		for attrib in attrib_set:
			values = [str(row[0]), quoted(attrib.encode("utf-8"))]
			write_to_db(write_cursor, TABLE_INFOBOX_ATTRIBUTES, columns, values)
	
	read_cursor.close()
	write_cursor.close()

def write_to_db(cursor, table_name, columns, values):
	query = "INSERT INTO " + table_name + \
				"(" + ", ".join(columns) + ")" \
				" VALUES ( " +  ", ".join(values) + " )"
	try:
		cursor.execute(query)
	except MySQLdb.Error, e:
		#it may be OK if dublicate. let it be processed higher
		if e.args[0] == ERROR_DUBLICATE_ENTRY:
			raise
		
		#show some error environment	
		print "EXEPTION in write_to_db"
		print "values = ", values
		#delete unnecessaty spaces for pretty report
		query = ' '.join(query.split())
		print "query = ", query
		raise



try:
	conn1 = MySQLdb.connect (host = "localhost",
									user = "alex",
									db = "ruwiki")

	conn1.set_character_set('utf8')
	
	conn2 = MySQLdb.connect (host = "localhost",
									user = "alex",
									db = "ruwiki")

	conn2.set_character_set('utf8')

	get_infobox_text(conn1, conn2)

	conn1.close ()
	conn2.close ()

except MySQLdb.Error, e:
	print "Error %d: %s" % (e.args[0], e.args[1])
	sys.exit (1)

